package com.Imp;

import com.entity.Customer;
import com.mapper.CustomerMapper;
import com.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomerMapperImpl implements CustomerMapper {
    public List<Customer> getAllCustomers() {
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            List<Customer> customers = sqlSession.selectList("com.mapper.CustomerMapper.getAllCustomers");
            return customers;
        }
    }

    public Customer getCustomerByUserName(String userName) {
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            Map<String, Object> param = new HashMap<>();
            param.put("userName", userName);
            return sqlSession.selectOne("com.mapper.CustomerMapper.getCustomerByUserName", userName);
        }
    }

    public void addCustomer(Customer customer) {
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            sqlSession.insert("com.mapper.CustomerMapper.addCustomer", customer);
            sqlSession.commit();
        }
    }

    public void updateCustomer(Customer customer) {
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            sqlSession.update("com.mapper.CustomerMapper.updateCustomer", customer);
            sqlSession.commit();
        }
    }

    public void deleteCustomer(String userName) {

        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            sqlSession.delete("com.mapper.CustomerMapper.deleteCustomer", userName);
            sqlSession.commit();
        }
    }
}
